<template>
  <div class="login">
    <!-- 左边 -->
    <div class="left">
      <div class="top">
        <div class="logo"><img src="@/assets/img/login_logo.png" alt="" /></div>
        <div class="t1">黑马面面</div>
        <span class="line"></span>
        <div class="t2">用户登录</div>
      </div>
      <!-- 表单 -->
      <el-form :model="form" :rules="rules" ref="form">
        <!-- 手机号 -->
        <el-form-item prop="phone">
          <el-input
            placeholder="请输入手机号"
            v-model="form.phone"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>

        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17">
              <el-input
                prefix-icon="el-icon-key"
                placeholder="请输入验证码"
                v-model="form.code"
              ></el-input>
            </el-col>
            <el-col :span="7">
              <img :src="codeUrl" alt="" class="image" @click="refreshCode"/>
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 勾选 -->
        <el-form-item prop="check">
          <el-checkbox v-model="form.check">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>
            和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>

        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" class="btn" @click="goLogin"
            >登录</el-button
          >
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            class="btn"
            @click="$refs.register.isShow = true"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!-- 右边 -->
    <div class="right">
      <img src="@/assets/img/login_banner_ele.png" alt="" />
    </div>
    <!-- 对话框 -->
    <register ref="register"></register>
  </div>
</template>

<script>
import register from "./register.vue";
import { localSet } from '@/utils/token.js'
import { login } from '@/api/login.js'
export default {
  components: {
    register,
  },
  data() {
    return {
      codeUrl: process.env.VUE_APP_URL + "/captcha?type=login",
      form: {
        phone: "", //电话
        password: "", //密码
        check: '', //选择
        code: "", //验证码
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (reg.test(value)) {
                callback();
              } else {
                callback("请正确输入电话号码");
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
           {
            min: 6,
            max: 12,
            message: "请输入6-12密码",
            trigger: "change",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" },
          {
            min: 4,
            max: 4,
            message: "请正确填写验证码位数",
            trigger: "change",
          },
        ],
        check: [{ required: true, message: "请选择勾选", trigger: "change" },
                {validator:(rule, value, callback) =>{
                  if(value === true){
                    callback()
                  }else{
                    callback("请勾选协议")
                  }
                },
                trigger:"change"}
        ],
      },
    };
  },
  methods: {
    // 全局验证
    goLogin() {
      this.$refs.form.validate((result) => {
        window.console.log(result);
        if (!result) {
          this.$message.warning("验证失败");
        } else {
          login(this.form).then(res =>{
            window.console.log("登录数据:",res); 
           localSet(res.data.token)
           this.$message.success("登录成功");
           window.console.log(res.data.token);
          //  路由调转
           this.$router.push('/layout')
          })
        }
      });
    },

    // 刷新验证码
    refreshCode(){
      this.codeUrl= process.env.VUE_APP_URL + "/captcha?type=login&t="+ Date.now()

    }
  },
};
</script>

<style lang="less">
.login {
  width: 100%;
  height: 100%;
  background: linear-gradient(
    225deg,
    rgba(20, 148, 250, 1),
    rgba(2, 198, 250, 1)
  );
  display: flex;
  //   水平
  justify-content: space-around;
  // 侧方向
  align-items: center;
  .left {
    box-sizing: border-box;
    width: 478px;
    height: 550px;
    background: rgba(245, 245, 245, 1);
    padding: 43px;

    .top {
      margin-bottom: 26px;
      display: flex;
      align-items: center;
      .logo {
        width: 22px;
        height: 17px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN;
        font-weight: 400;
        color: rgba(13, 12, 12, 1);
        margin: 0 14px 0 17px;
      }
      .line {
        width: 1px;
        height: 28px;
        margin-right: 14px;
        background: rgba(199, 199, 199, 1);
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC;
        font-weight: 400;
        color: rgba(13, 12, 12, 1);
      }
    }
    .image {
      width: 100%;
      height: 40px;
    }
    .el-col-7 {
      height: 40px;
    }
    .el-button {
      width: 394px;
      &:nth-child(1) {
        margin-bottom: 20px;
      }
    }
  }
  .right {
    width: 633px;
    height: 435px;
  }
}
</style>
